package com.estronger.shopmanage.ui.aftermarket;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.fragment.FragmentFactory;
import com.estronger.shopmanage.fragment.install.BuildBaseFragment;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.ui.aftermarket.adapter.MeasureAdpater;
import com.estronger.shopmanage.ui.aftermarket.bean.TaskNumBean;
import com.estronger.shopmanage.ui.aftermarket.fragment.AftermarketBaseFragment;
import com.estronger.shopmanage.ui.aftermarket.utils.AftermarketUitl;
import com.estronger.shopmanage.ui.install.BuildActivity;
import com.google.gson.Gson;
import com.lidroid.xutils.http.RequestParams;
import com.viewpagerindicator.TabPageIndicator;

import java.util.Map;

public class AftermarketActivity extends BaseActivity {


    public static int REFLESH_DATA = 5000;//刷新数据

    public static String send_deliv_id;//页面开始跳转其他页面时给值，用来回调跳转参数

    @BindView(R.id.category_strip)
    TabPageIndicator indicator;// 页面的title布局
    @BindView(R.id.main_vp)
    ViewPager viewPager;
    @BindView(R.id.copy_task_state)
    ImageView getState;
    @BindView(R.id.progressBar1)
    ProgressBar progressbar;

    MeasureAdpater measureAdpater;

    int currPosition = 0;
    AftermarketUitl aftermarketUitl;
    Gson gson = new Gson();

    @Override
    protected void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        FrameLayout frameLayout = (FrameLayout) View.inflate(this, R.layout.activity_aftermarket_layout,
                null);
        setContentView(frameLayout);
        ButterKnife.bind(this);
        aftermarketUitl = new AftermarketUitl(this);
    }

    @Override
    protected void initAction() {
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(final int position) {
                currPosition = position;
                MyApplication.getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                    }
                }, 500);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        measureAdpater = new MeasureAdpater(getSupportFragmentManager(),aftermarketUitl.getViewBeanList());
        viewPager.setAdapter(measureAdpater);
        indicator.setViewPager(viewPager);
        indicator.onPageSelected(0);
        indicator.setSmoothScrollingEnabled(true);
        getTitleCount();
    }

    /*
    * 获取数量
    * */
    public void getTitleCount(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("sf_action", "claimgettasklistnum");
        params.addBodyParameter("usercode", MyApplication.getUserCode());
        connect(params, NetManager.AFTERMARKET_NUM);
    }

    @Override
    protected void onNetSuccess(int code, String result) {
        super.onNetSuccess(code, result);
        if (code == NetManager.AFTERMARKET_NUM){
            TaskNumBean taskNumBean = gson.fromJson(result,TaskNumBean.class);
            if (taskNumBean.isSuccess() && aftermarketUitl.getViewBeanList().size() ==3){
                aftermarketUitl.getViewBeanList().get(0).setTitle("预约确认(" + taskNumBean.getData().getFconfirmnum() + ")");
                aftermarketUitl.getViewBeanList().get(1).setTitle("完工确认(" + taskNumBean.getData().getFfinishnum() + ")");
                aftermarketUitl.getViewBeanList().get(2).setTitle("全部(" + taskNumBean.getData().getFallnum() + ")");
                setIndicatorTitle(0);
                setIndicatorTitle(1);
                setIndicatorTitle(2);
            }
        }
    }

    void setIndicatorTitle(int position) {
        LinearLayout layout = ((LinearLayout) indicator.getChildAt(0));
        TextView tv = (TextView) (layout.getChildAt(position));
        tv.setText(aftermarketUitl.getViewBeanList().get(position).getTitle());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100){
            Intent intent = new Intent(this,AppointmentSureActivity.class);
            intent.putExtra("taskId",AftermarketActivity.send_deliv_id);
            startActivityForResult(intent,REFLESH_DATA);
        }else if (resultCode == REFLESH_DATA){
            getTitleCount();
            for (AftermarketUitl.ViewBean viewBean : aftermarketUitl.getViewBeanList()){
                AftermarketBaseFragment fragment = (AftermarketBaseFragment)viewBean.getFragment();
                fragment.onRequstData();
            }
        }

    }
}
